/*
* Serhiy Bondarenko
* Task 3. Anagram
*/
import java.util.*;
import java.io.*;
	
/**
* Class Anagram outputs to screen
* answer to the question: "Are this words(phrases) anagrams?"
* @version 1.2 (28 Jan 2013)
* @author Serhiy Bondarenko
*/
public class Anagram {
    
public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    boolean answer = areAnagrams(inputPhrase("first", in),
                                 inputPhrase("second", in));
    printAnswer(answer);
    in.close();
}

private static String inputPhrase(String numberOfPhrase,Scanner in) {
    System.out.println("Enter " + numberOfPhrase + " phrase:");
    return in.nextLine();
}

private static String formatPhrase(String phrase) {
    String SPACE = "\\s";
    return phrase.toLowerCase().replaceAll(SPACE, "");
}

private static int[] getCodePoints(String phrase) {
    int codePointsNumber = phrase.codePointCount(0, phrase.length());
    int[] codePoints = new int[codePointsNumber];    
    
    for (int offset = 0; offset < codePoints.length; offset++) {
        int pointIndex = phrase.offsetByCodePoints(0, offset);
        codePoints[offset] = phrase.codePointAt(pointIndex);
    }
    return codePoints;
}

private static boolean areAnagrams(String firstPhrase, String secondPhrase) {
    int[] phraseOneCodes = getCodePoints(formatPhrase(firstPhrase));
    int[] phraseTwoCodes = getCodePoints(formatPhrase(secondPhrase));
    Arrays.sort(phraseOneCodes);
    Arrays.sort(phraseTwoCodes);
    return Arrays.equals(phraseOneCodes, phraseTwoCodes);
}

private static void printAnswer(boolean areAnagrams) {
    if ( areAnagrams ) {
        System.out.println("Yes, this two phrases are anagrams");
    } else {
        System.out.println("No, this two phrases are not anagrams");
    }
}
}